<?php
class smarty_ext_shop {

	function smarty_ext_shop() {
		global $setupdata, $mysqlclass, $shopclass;
		$this->setupdata=$setupdata;
		$this->mysqlclass=$mysqlclass;
		$this->shopclass=$shopclass;
	}
	
	function get_all_product_groups(){
		return $this->shopclass->get_all_product_groups();
	}
	
	function get_all_types_of_group(){
		return $this->shopclass->get_all_types_of_group();
	}

	function get_product_group_fields($params){
		if ((isset($params['group']) && (strlen($params['group']) > 0))){
			return $this->shopclass->get_product_group_fields($params['group']);
		}
	}
	
	function get_product_group_data($params){
		if ((isset($params['group']) && (strlen($params['group']) > 0))){
			$group_info = $this->shopclass->get_product_group_data($params['group']);
//			$price_mod = $this->shopclass->get_price_mod_by_product_id($params['group'], 'shop_group');
			$price_mod = $this->shopclass->get_off($params['group'], 'shop_group');
			if ($price_mod != 0) $group_info['price_mod'] = $price_mod;
			return $group_info;
		}
	}
	
	function get_all_products_by_group($params){
		if ((isset($params['group_id']) && (strlen($params['group_id']) > 0))){
			if ((isset($params['hide']) && (strlen($params['hide']) > 0))) {
				return $this->shopclass->get_all_products_by_group($params['group_id'], $params['hide']);
			} else {
				return $this->shopclass->get_all_products_by_group($params['group_id']);
			}
		}
	}
	
	function get_all_products_by_group_name_only($params){
		if ((isset($params['group_id']) && (strlen($params['group_id']) > 0))){
			if (!isset($params['sort']) || (($params['sort'] != 'down') && ($params['sort'] != 'up'))) $params['sort'] = 'no';
			if ((isset($params['hide']) && (strlen($params['hide']) > 0))) {
				if (isset($params['curent_page']) && (strlen($params['curent_page']) > 0)) {
					return $this->shopclass->get_all_products_by_group_name_only($params['group_id'], $params['hide'], $params['curent_page'], $params['sort']);
				} else {
					return $this->shopclass->get_all_products_by_group_name_only($params['group_id'], $params['hide'], 1, $params['sort']);
				}
			} else {
				if (isset($params['curent_page']) && (strlen($params['curent_page']) > 0)) {
					return $this->shopclass->get_all_products_by_group_name_only($params['group_id'], $params['hide'], $params['curent_page'], $params['sort']);
				} else {
					return $this->shopclass->get_all_products_by_group_name_only($params['group_id'], "", 1, $params['sort']);
				}
			}
		}
	}
	
	function get_product_fields($params){
		if ((isset($params['product_id'])) && (strlen($params['product_id']) > 0)){
			return $this->shopclass->get_product_fields($params['product_id']);
		}
	}
	
	function get_product_field_data($params){
		return $this->shopclass->get_product_field_data($params['parent'], $params['name'], $params['lang']);
	}
	
	function get_product_fields_with_data($params){
		if ((isset($params['product_id'])) && (strlen($params['product_id']) > 0)){
			$product_info = $this->shopclass->get_product_fields_with_data($params['product_id'], $_SESSION['lang']);
			$price_mod = $this->shopclass->get_price_mod_by_product_id($params['product_id'], 'shop_product');
			if ($price_mod != 0) $product_info['price_mod'] = $price_mod;
			return $product_info;
		}
	}
	
	function get_product_fields_with_data_and_type($params){
		if ((isset($params['product_id'])) && (strlen($params['product_id']) > 0)){
			$product_info = $this->shopclass->get_product_fields_with_data_and_type($params['product_id'], $_SESSION['lang']);
			$price_mod = $this->shopclass->get_price_mod_by_product_id($params['product_id'], 'shop_product');
			if ($price_mod != 0) $product_info['price_mod'] = $price_mod;
			return $product_info;
		}
	}
	
	function get_products_by_search($params){
	 	if (!isset($_GET['cache_id'])) $product_cache_id=NULL; else $product_cache_id=$_GET['cache_id'];
		if ($product_cache_id==NULL) if (!isset($_POST['cache_id'])) $product_cache_id=NULL; else $product_cache_id=$_POST['cache_id'];
		if ((isset($params['group_id'])) && (strlen($params['group_id']) > 0)) $group = $params['group_id'];
		else $group = "0";
		if ((isset($params['search'])) && (strlen($params['search']) > 0)) {
			$query[1]['value'] = $params['search'];
		}
		if (isset($query) && (is_array($query)) && (count($query) > 0)) {
			$tmp = $this->shopclass->products_search($query, "0", $group, 1);
			$data['search']['products'] = $tmp['products'];
			$data['search']['cache_id'] = $tmp['product_search_cache_id'];
			$data['search']['query'] = $query;
		} elseif ($product_cache_id != NULL){
			$tmp = $this->shopclass->products_search(array(), $product_cache_id, $group, 1);
			$data['search']['products'] = $tmp['products'];
			$data['search']['cache_id'] = $tmp['product_search_cache_id'];
			$data['search']['query'] = $tmp['query'];
		} else $data['search']['products'] = "";
		return $data;
	}
	
	function get_curency(){
		return $this->shopclass->get_curency();
	}
		
	function get_all_discounts(){
		global $shopclass;
		return $shopclass->offs;
	}
	
	function get_all_modules($params){
		if (!isset($this->setupdata['modules']['shop']['modules'])) $shop_modules = array();
		else $shop_modules = $this->setupdata['modules']['shop']['modules'];
		if (!isset($params['user']) || ((intval($params['user'])) == 0)) return $shop_modules;
		global $usersclass;
		$tmp2 = $this->shopclass->get_all_product_groups();
		$tmp = $shop_modules;
		$user_info = $usersclass->GetUserInfo($params['user'], "admins");
		if (($params['user'] == 9999999) || ($user_info['mod']['1'] == 1)) {
			if (count($tmp) > 0) foreach($tmp as $key=>$value) $shop_modules[$key] = 1;
			if (count($tmp2) > 0) foreach($tmp2 as $key=>$value) $shop_modules['product_groups'][$key] = 1;
			return $shop_modules;
		} else {
			if (count($tmp) > 0) foreach($tmp as $key=>$value) $shop_modules[$key] = 0;
			if (count($tmp2) > 0) foreach($tmp2 as $key=>$value) $shop_modules['product_groups'][$key] = 0;
		}
		
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."users_depend` WHERE `users_id`=".$params['user']." && `module` LIKE 'shop_module_%'", __LINE__, __FILE__);
		while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
			if ($row['module'] != 'shop_module_group'){
				$row['module'] = substr($row['module'], strlen("shop_module_"), strlen($row['module'])-strpos($row['module'], "shop_module_"));
				if (array_key_exists($row['module'],$shop_modules)) $shop_modules[$row['module']] = 1;
			} else {
				$shop_modules['product_groups'][$row['page_id']] = 1;
			}
		}
		
		if (isset($shop_modules['product_groups'])){
			foreach($shop_modules['product_groups'] as $key=>$value){
				if ($value == 1){
					if ($tmp2[$key]['parent'] != 0) $shop_modules['product_groups'][$tmp2[$key]['parent']] = 1;
				}
			}
		}
		
		return $shop_modules;
	}
	
	function get_off_global_data(){
		return $this->shopclass->get_off_global_data();
	}
	
	function get_all_off_groups($params){
		if ((isset($params['type'])) && (strlen($params['type']) > 0)){
			return $this->shopclass->get_all_off_groups($params['type']);
		} else return $this->shopclass->get_all_off_groups();
	}
	
	function get_off_data($params){
		if ((isset($params['off_id'])) && (strlen($params['off_id']) > 0)){
			return $this->shopclass->get_off_data($params['off_id']);
		}
	}

	function get_assigned_discount($params){
		if ((isset($params['parent'])) && (strlen($params['parent']) > 0) && (isset($params['type'])) && (strlen($params['type']) > 0)){
			return $this->shopclass->get_off($params['parent'], $params['type']);
		}
	}
	
	function get_assigned_discount_adv($params){
		if ((isset($params['parent'])) && (strlen($params['parent']) > 0) && (isset($params['type'])) && (strlen($params['type']) > 0)){
			return $this->shopclass->get_off_adv($params['parent'], $params['type']);
		}
	}
	
	function get_super_select($params){
		return $this->shopclass->get_super_select($params);
	}
	
	function get_page_id_from_product_group_id($params){
		if (isset($params['parent']) && (strlen($params['parent']) > 0)){
			if  (!isset($params['lang']) || (strlen($params['lang']) == 0)) $params['lang'] = $_SESSION['lang'];
			return $this->shopclass->get_page_id_from_product_group_id($params['parent'], $params['lang']);
		}
	}
	
	function get_group_count_data($params){
		if (isset($params['main']) && (strlen($params['main']) > 0)) $main = intval($params['main']);
		else $main = 0;
		if (isset($params['sub']) && (strlen($params['sub']) > 0)) $sub = intval($params['sub']);
		else $sub = 0;
		return $this->shopclass->get_group_count_data($main, $sub);
	}
	
	function get_all_product_groups_one_product_type($params){
		if (isset($params['type']) && (strlen($params['type']) > 0)) {
			if (isset($params['groups']) && is_array($params['groups'])){
				$result = array();
				foreach($params['groups'] as $key=>$value){
					if (($value['parent'] != 0) && ($value['type']==$params['type'])) $result[$key]=$value;
				}
				foreach($result as $key1=>$value1){
					foreach($params['groups'] as $key2=>$value2){
						if ($value1['parent'] == $key2) $result[$key2]=$value2;
					}
				}
				return $result;
			} else return $params['groups'];
		} else return $params['groups'];
	}
	
	function get_text_for_container($params){
		$all = $this->shopclass->get_all_product_groups();
		$value = "";
		$t1 = explode(";", $params['value']);
		foreach ($t1 as $v1){
			if ($v1 != ""){
				$t2 = explode("=", $v1);
				foreach ($t2 as $v2){
					if ($v2 != ""){
						if(isset($all[$v2])) {
							$value .= $all[$v2]['name']." => ";
							$t3 = explode(":", $t2[1]);
							$v3 = $t3[0];
							if ($v3 != ""){
								if(isset($all[$v3])) {
									$value .= $all[$v3]['name']."\n";
									$t4 = explode(",", $t3[1]);
									foreach ($t4 as $v4){
										if ((intval($t2[0]) > 0) && (intval($v4) > 0)) {
											$x = $this->shopclass->get_product_fields_with_data_and_type($v4, $_SESSION['lang']);
											$value .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".$x['product_name']."\n";
										}
									}
								}
							}
						}
					}
				}
			}
		}
		return $value;
	}
	
	function get_permisions_container($params){
		$result = array();
		$value = $this->shopclass->get_product_field_data($params['parent'], $params['name'], $params['lang']);
		$t1 = explode(";", $value);
		foreach ($t1 as $v1){
			$t2 = explode("=", $v1);
			$t3 = explode(":", $t2[1]);
			$t4 = explode(",", $t3[1]);
			foreach ($t4 as $v4){
				if (intval($v4) > 0) $result[$v4] = "on";
			}
		}
		return $result;
	}
	
	function get_all_products_by_group_name_only_fulllist($params){
		$result = array();
		if ((isset($params['group_id']) && (strlen($params['group_id']) > 0))){
			$group_id = intval($params['group_id']);
			$q = "SELECT `t1`.`id`, `t1`.`name` FROM `".$this->setupdata['table_prefix']."product_catalog` `t1` WHERE ".$group_id." = `t1`.`parent`";
			$q .=  "AND `t1`.`hide`=0 ORDER by `t1`.`pos`";
			
			$result = array();
			$sql = $this->mysqlclass->mysql_my_query($q, __LINE__, __FILE__);
			while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
				$result[$row['id']] = $row['name'];
			}
		}
		return $result;
	}
}
?>